IRCBot::Plugin.define "poll" do
	help_string 'Creates polls. Only the poll starter may end it. Create a poll (in the channel) by saying "poll start The Question? [ ] Answer 1 [ ] Answer 2 [ ]..." ad nauseum. Vote by saying "/me votes option #". End a poll by saying "poll end", check its status with "poll status", unvote with "/me unvotes", and check its alternatives with "poll options"'
	@polls = []
	class Poll
		attr_reader :nick,:starter,:channel,:votes,:question,:options
		def initialize(starter,channel,options,question)
			@starter = starter.split('!')[1]
			@nick = starter.split('!')[0]
			@channel = channel.downcase
			@options = options
			@votes = []
			@voters = {}
			@question = question
			puts question
			puts options.join(', ')
		end
		
		def vote_by_number(option_number,voter)
			option = option_number - 1
			if @options[option] then
				if !@votes[option] then @votes[option] = 0; end
				if @voters[voter] then
					@votes[@voters[voter]] -= 1
					@voters[voter] = option
					@votes[option] += 1
				else
					@voters[voter] = option
					@votes[option] += 1
				end
				return true
			else
				return false
			end
		end
		
		def vote(option,voter)
			puts option
			option = @options.index(option)
			puts option
			if option then
				if !@votes[option] then @votes[option] = 0; end
				if @voters[voter] then
					@votes[@voters[voter]] -= 1
					@voters[voter] = option
					@votes[option] += 1
				else
					@voters[voter] = option
					@votes[option] += 1
				end
				return true
			else
				return false
			end
		end
		
		def unvote(voter)
			if @voters[voter] then
				@votes[@voters[voter]] -= 1
				@voters[voter] = nil
				return true
			else
				return false
			end
		end
	end
	
	def command(user,target,message,hostmask)
		command = message.split[0]
		message = message.sub(command,'').strip
		case command
			when 'start'
				my_poll = get_poll(target)
				if my_poll then
					privmsg(target,"There's already a poll running in this channel.")
				else
					split_message = message.split('[ ]')
					if split_message.count == 1 then
						privmsg(target,"Please, give me some options.")
					else
						question = split_message[0]
						split_message.delete question
						question.strip!
						answers = []
						for answer in split_message
							answers.push answer.strip.downcase
						end
						@polls.push Poll.new(hostmask,target,answers,question)
						privmsg(target,"Poll started in #{target}! The question is: \"#{question}\"")
						my_poll = get_poll(target)
						output = "Options: "
						for answer in my_poll.options
							output << "[#{my_poll.options.index(answer)+1}] #{answer.capitalize}, "
						end
						output = output.strip
						output[-1] = '.'
						privmsg(target,output)
					end
				end
			when 'end'
				my_poll = get_poll(target)
				if my_poll then
					if my_poll.starter == hostmask.split('!')[1] then
						@polls.delete my_poll
						privmsg(target,"Poll \"#{my_poll.question}\" ended!")
						output = "Results: "
						for option in my_poll.options
							if !my_poll.votes[my_poll.options.index(option)] then !my_poll.votes[my_poll.options.index(option)] = 0; end
							output << "#{option}: #{my_poll.votes[my_poll.options.index(option)]}, "
						end
						output.strip!
						output[-1] = '.'
						privmsg(target,output)
					else
						privmsg(target,"You can't end the poll!")
					end
				else
					privmsg(target,"There's no poll.")
				end
			when 'options'
				my_poll = get_poll(target)
				if my_poll then
					output = "Options: "
					for answer in my_poll.options
						output << "[#{my_poll.options.index(answer)+1}] #{answer.capitalize}, "
					end
					output = output.strip
					output[-1] = '.'
					privmsg(target,output)
				else
					privmsg(target,"There's no poll.")
				end
			when 'status'
				my_poll = get_poll(target)
				if my_poll then
					output = "Status: "
					for option in my_poll.options
						if !my_poll.votes[my_poll.options.index(option)] then !my_poll.votes[my_poll.options.index(option)] = 0; end
						output << "#{option}: #{my_poll.votes[my_poll.options.index(option)]}, "
					end
					output = output.strip
					output[-1] = '.'
					privmsg(target,output)
				else
					privmsg(target,"Poll? What poll?")
				end
		end
	end
	
	def privmsg_function(user,target,message,hostmask)
		my_poll = get_poll(target)
		if my_poll
			if message.start_with?("#{1.chr}ACTION votes option ") then
				option = message.sub("#{1.chr}ACTION votes option ",'').to_i
				result = my_poll.vote_by_number(option,hostmask.split('!')[1])
				if result then
					privmsg(target,"Vote added.")
				else
					privmsg(target,"Invalid opinion.")
				end
			elsif message == "#{1.chr}ACTION unvotes#{1.chr}" then
				result = my_poll.unvote(hostmask.split('!')[1])
				if result
					privmsg(target,"Vote removed.")
				else
					privmsg(target,"You haven't even voted!")
				end
			end
		end
	end
	def loop_hook()
		for poll in @polls
			if !$ircbot.users[poll.channel].include?(poll.nick.downcase) then
				@polls.delete poll
				privmsg(poll.channel,"Poll destroyed, starter left the channel.")
				output = "Results: "
				for option in poll.options
					if !poll.votes[poll.options.index(option)] then !poll.votes[poll.options.index(option)] = 0; end
					output << "#{option}: #{poll.votes[poll.options.index(option)]}, "
					output = output.strip
					output[-1] = '.'
				end
				privmsg(target,output)
			end
		end
	end
	
	def get_poll(target)
		has_poll = false
		my_poll = nil
		for poll in @polls
			if poll.channel == target.downcase then
				my_poll = poll
				has_poll = true
			end
		end
		return my_poll
	end
end
